local borui = fk.CreateSkill{
  name = "hanqing__borui",
}

Fk:loadTranslationTable{
  ["hanqing__borui"] = "拨锐",
  [":hanqing__borui"] = "每轮限两次，一张伤害牌指定目标时，若你为目标或在此牌最短路径，你可以从弃牌堆展示一张可响应之的牌，令使用者选择一项："..
  "1.取消伤害牌，获得展示牌；2.令目标角色获得展示牌，你获得伤害牌。",

  ["#hanqing__borui-invoke"] = "拨锐：展示弃牌堆一张可以响应%arg的牌，令 %dest 选择一项",
  ["hanqing__borui1"] = "取消%arg，你获得%arg2",
  ["hanqing__borui2"] = "令一名目标获得%arg2，你收回%arg",
  ["#hanqing__borui-choose"] = "拨锐：令一名目标获得%arg",
}

local function onRoad(player, from, to)
  local left, right, on_right = 0, 0, false
  if player == to then
    return true
  end
  if player == from or to.dead or player:isRemoved() or from:isRemoved() or to:isRemoved() then return end
  local temp = from
  while temp ~= to do
    if not temp.dead then
      right = right + 1
      if temp == player then
        on_right = true
      end
    end
    temp = temp.next
  end
  left = #player.room.alive_players - right
  if math.min(left, right) > 1 then
    if left > right then
      return on_right
    elseif left < right then
      return not on_right
    end
    return true
  end
  return false
end

local mapper = {
  slash = { "jink", "borrow_arrows" },
  duel = { "slash", "nullification", "borrow_arrows" },
  savage_assault = { "slash", "nullification", "borrow_arrows" },
  collateral = { "slash", "nullification", "borrow_arrows" },
  wenhe_chaos = { "slash", "nullification", "borrow_arrows" },
  archery_attack = { "jink", "nullification", "borrow_arrows" },
  raid_and_frontal_attack = { "slash", "jink", "nullification", "borrow_arrows" },
}

borui:addEffect(fk.TargetSpecifying, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(borui.name) and
      data.card.is_damage_card and data.firstTarget and not target.dead and
      table.find(data.use.tos, function (p)
        return onRoad(player, target, p)
      end) and
      player:usedSkillTimes(borui.name, Player.HistoryRound) < 2 and
      table.find(player.room.discard_pile, function (id)
        return table.contains(mapper[data.card.trueName] or {}, Fk:getCardById(id).trueName)
      end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = table.filter(room.discard_pile, function (id)
      return table.contains(mapper[data.card.trueName] or {}, Fk:getCardById(id).trueName)
    end)
    local card = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      include_equip = false,
      skill_name = borui.name,
      pattern = tostring(Exppattern{ id = cards }),
      prompt = "#hanqing__borui-invoke::"..target.id..":"..data.card:toLogString(),
      cancelable = true,
      expand_pile = cards,
    })
    if #card > 0 then
      event:setCostData(self, {cards = card, tos = {target}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local id = event:getCostData(self).cards[1]
    room:showCards(id)
    if target.dead then return end
    local choices = { "hanqing__borui1:::"..data.card:toLogString()..":"..Fk:getCardById(id):toLogString() }
    if table.find(data.use.tos, function (p)
      return not p.dead
    end) and
      table.contains(room.discard_pile, id) then
      table.insert(choices, "hanqing__borui2:::"..data.card:toLogString()..":"..Fk:getCardById(id):toLogString())
    end
    local choice = room:askToChoice(target, {
      choices = choices,
      skill_name = borui.name,
    })
    if choice:startsWith("hanqing__borui1") then
      data:cancelAllTarget()
      if table.contains(room.discard_pile, id) then
        room:moveCardTo(id, Card.PlayerHand, target, fk.ReasonJustMove, borui.name, nil, true, player)
      end
    else
      local tos = table.filter(data.use.tos, function (p)
        return not p.dead
      end)
      if #tos > 1 then
        tos = room:askToChoosePlayers(player, {
          min_num = 1,
          max_num = 1,
          targets = tos,
          skill_name = borui.name,
          prompt = "#hanqing__borui-choose:::"..Fk:getCardById(id):toLogString(),
          cancelable = false,
        })
      end
      room:moveCardTo(id, Card.PlayerHand, tos[1], fk.ReasonJustMove, borui.name, nil, true, player)
      if not target.dead and room:getCardArea(data.card) == Card.Processing then
        room:moveCardTo(data.card, Card.PlayerHand, target, fk.ReasonJustMove, borui.name, nil, true, player)
      end
    end
  end,
})

return borui
